package com.example.demo2.student;

import com.example.demo2.Util.DBHelper;
import com.example.demo2.pojo.class1;
import com.example.demo2.pojo.grade;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(value = "/selectgradeListSevlert")
public class selectgradeListSevlert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PreparedStatement preparedStatement = null;
        Connection conn = null;
        ResultSet rs = null;
        DBHelper dBHelper = new DBHelper();
     conn = dBHelper.getConn();
        ArrayList<grade> class1s = new ArrayList<>();
        try {
            preparedStatement = conn.prepareStatement("select s.sid,s.name,s.class,m.name,s.fnum,m.credit,s.num from webdesign.student s left join webdesign.major m on s.majorid = m.id");
            rs = preparedStatement.executeQuery();
            while (rs.next()){
                grade grade = new grade();
                grade.setId(rs.getInt("sid"));
                grade.setName(rs.getString("s.name"));
                grade.setClasss(rs.getString("class"));
                grade.setMajor(rs.getString("m.name"));
                grade.setCreat(rs.getDouble("s.fnum"));
                grade.setAllCreat(rs.getDouble("m.credit"));
                grade.setGrade(rs.getDouble("s.num"));
                class1s.add(grade);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dBHelper.closeAll(conn,preparedStatement,rs);
        }
        req.setAttribute("list", class1s);
        RequestDispatcher dispatcher = req.getRequestDispatcher("gradeList.jsp");
        dispatcher.forward(req, resp);
    }
}
